package com.yw.atp.action;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.yw.atp.model.Group;
import com.yw.atp.model.Profession;
import com.yw.atp.model.Staff;
import com.yw.atp.service.PersistenceService;
import com.yw.atp.util.StringUtil;
import com.yw.atp.web.ActionBase;

@Scope("prototype")
@Component("croupAction")
public class GroupAction extends ActionBase implements ServletRequestAware{
	private static final long serialVersionUID = 1L;
	@Autowired
	private PersistenceService service;
	HttpServletRequest request;
	private Group group;
	private List<Group> groupList;
	private String res;
	private int start  = 0;
	private int limit  = 20;
	private String ids;
	
    public String list(){
    	groupList = service.findAll(Group.class);
		JSONObject js = new JSONObject();
		try {
			if(groupList!=null&&groupList.size()>0){
				js.put("totalCount",groupList.size());
				int len = groupList.size();
				start = start>(len-1)?(len-1):start;
				limit = limit>(len-start)?(len):(start+limit);
				js.put("topics",Group.capture(groupList.subList(start,limit)));
			}
			res = js.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String add(){
		if(StringUtil.checkGroup(group)){
			if(group.getId()!=null&&group.getId().intValue()>0){
				group.setUpdateAt(new Date());
			}else{
				group.setCreatedAt(new Date());
			}
			service.save(group);
			return SUCCESS;
		}
		return INPUT;
	}
	
	public String del(){
		if(ids!=null&&!"".equals(ids)){
			service.remove(Group.class.getName(),ids);
		}
		return SUCCESS;
	}
	
	public void setServletRequest(HttpServletRequest request) {
        if(request==null){
            request = ServletActionContext.getRequest();
        }
        this.request = request;
    }
	
	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}
	
	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getRes() {
		return res;
	}

	public void setRes(String res) {
		this.res = res;
	}

	public PersistenceService getService() {
		return service;
	}

	public void setService(PersistenceService service) {
		this.service = service;
	}
	
	public List<Group> getGroupList() {
		return groupList;
	}

	public void setGroupList(List<Group> groupList) {
		this.groupList = groupList;
	}
}
